package com.cetcs.kmga.dataManager.controller;

import com.alibaba.fastjson.JSON;
import com.cetc.cloud.kmga.util.HttpUtil;
import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;

import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataManager.service.device.DeviceManagerService;
import com.cetcs.kmga.dataManager.util.ReadConfigUtil;

import deviceservice.entity.reponse.DevAddResp;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Writer;

/**
 * 类注释
 * 日志管理
 * @author yyangs
 * @version V1.0 创建时间：2017-08-30 13:40
 * Copyright 2017 by CETC
 */
@RestController
@RequestMapping("/test")
public class TestController extends BaseController {
    @Autowired
    private DeviceManagerService deviceManagerService;

    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse test(){
        try {
            deviceManagerService.createDataResource(new Data());
            return this.ok(1);
        } catch (Exception e) {
            return this.error(500);
        }
    }

    @RequestMapping(value = "/req", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public void handleRequest(Writer writer, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        HttpUtil httpUtil = new HttpUtil("http://192.168.112.200/findLicense.action");
        LoginEntity loginEntity = new LoginEntity();
        String data = JSON.toJSONString(loginEntity);
        if(httpUtil.sendPostRequest(data,httpUtil.APPLICATION_JSON)){
            writer.write(httpUtil.getResponseString());
//            resp.sendRedirect("http://192.168.112.200/index.jsp");
        }
//        LoginEntity loginEntity = new LoginEntity();
//        byte[] requestBytes = JSON.toJSONString(loginEntity).getBytes("utf-8"); // 将参数转为二进制流
//        HttpClient httpClient = new HttpClient();// 客户端实例化
//        PostMethod postMethod = new PostMethod("http://192.168.112.200");
//        //设置请求头Authorization
//        postMethod.setRequestHeader("Authorization", "Basic ");
//        // 设置请求头  Content-Type
//        postMethod.setRequestHeader("Content-Type", "application/json");
//        InputStream inputStream = new ByteArrayInputStream(requestBytes, 0,
//                requestBytes.length);
//        RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
//                requestBytes.length, "application/json; charset=utf-8"); // 请求体
//        postMethod.setRequestEntity(requestEntity);
//        httpClient.executeMethod(postMethod);// 执行请求
//        InputStream soapResponseStream = postMethod.getResponseBodyAsStream();// 获取返回的流
//        byte[] datas = null;
//        try {
//            datas = readInputStream(soapResponseStream);// 从输入流中读取数据
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        String result = new String(datas, "UTF-8");// 将二进制流转为String
        // 打印返回结果

//        HttpUtil httpUtil = new HttpUtil(postURL);
//
//        String data = JSON.toJSONString(devAddReq1);
//        if(httpUtil.sendPostRequest(data,httpUtil.APPLICATION_JSON)){
//            LOG.info("[server] send devID request success");
//            DevAddResp devAddResp1 = JSON.parseObject(httpUtil.getResponseString(),DevAddResp.class);
//            if(devAddResp1.getResult()==100){//客户端验证成功，并且成功接收devID
//                LOG.info("[server] the authentication of client about devID is  successful");
//                addDevResult.setResult(true);
//                addDevResult.setAccessToken(access_token);
//                addDevResult.setDevID(deviceID);
//            }
//            else{
//                addDevResult.setResult(false);
//                LOG.error("[server] the authentication of client about devID is  FAILED : "+"[");
//            }
//        }
//        else{
//            addDevResult.setResult(false);
//            LOG.error("[server] send devID request FAILED");
//        }
    }


    /**
     * 从输入流中读取数据
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
}
